package com.cn.controller;

import com.cn.pojo.Admin;
import com.cn.service.AdminService;
import com.cn.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private JwtUtil jwtUtil;

    @PostMapping("/login")
    public String login(@RequestBody Admin admin){
        Admin loginUser= adminService.findByLogin(admin);
        if (null == loginUser){
            return "登陆失败";
        }
        //生成令牌
        String token = JwtUtil.createToken();
        return token;
    }

    @GetMapping("/helloWorld")
    public String helloWorld(@RequestHeader("Authorization") String authorization){
        if (jwtUtil.checkToken(authorization)) {
            return "你好";
        }
        return "失败";
    }

}
